home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / newmat03.lha / newmat03 / newmatrm.cxx < prev    next >
C/C++ Source or Header  |  1993-08-08  |  3KB  |  115 lines

  1. //$$newmatrm.cxx                         rectangular matrix operations
  2.  
  3. // Copyright (C) 1991: R B Davies and DSIR
  4.  
  5. #include "include.hxx"
  6.  
  7. #include "newmat.hxx"
  8. #include "newmatrm.hxx"
  9.  
  10.  
  11. // operations on rectangular matrices
  12.  
  13.  
  14. void RectMatrixRow::Reset (const Matrix& M, int row, int skip, int length)
  15. {
  16.    RectMatrixRowCol::Reset
  17.       ( M.Store()+row*M.Ncols()+skip, length, 1, M.Ncols() );
  18. }
  19.  
  20. void RectMatrixRow::Reset (const Matrix& M, int row)
  21. {
  22.    RectMatrixRowCol::Reset( M.Store()+row*M.Ncols(), M.Ncols(), 1, M.Ncols() );
  23. }
  24.  
  25. void RectMatrixCol::Reset (const Matrix& M, int skip, int col, int length)
  26. {
  27.    RectMatrixRowCol::Reset
  28.       ( M.Store()+col+skip*M.Ncols(), length, M.Ncols(), 1 );
  29. }
  30.  
  31. void RectMatrixCol::Reset (const Matrix& M, int col)
  32.    { RectMatrixRowCol::Reset( M.Store()+col, M.Nrows(), M.Ncols(), 1 ); }
  33.  
  34.  
  35. real RectMatrixRowCol::SumSquare() const
  36. {
  37.    long_real sum = 0.0; int i = n; real* s = store; int d = spacing;
  38.    while (i--) { sum += (long_real)*s * *s; s += d; }
  39.    return (real)sum;
  40. }
  41.  
  42. real RectMatrixRowCol::operator*(const RectMatrixRowCol& rmrc) const
  43. {
  44.    long_real sum = 0.0; int i = n;
  45.    real* s = store; int d = spacing;
  46.    real* s1 = rmrc.store; int d1 = rmrc.spacing;
  47.    if (i!=rmrc.n) MatrixError("Row/column length error in *");
  48.    while (i--) { sum += (long_real)*s * *s1; s += d; s1 += d1; }
  49.    return (real)sum;
  50. }
  51.  
  52. void RectMatrixRowCol::AddScaled(const RectMatrixRowCol& rmrc, real r)
  53. {
  54.    int i = n; real* s = store; int d = spacing;
  55.    real* s1 = rmrc.store; int d1 = rmrc.spacing;
  56.    if (i!=rmrc.n) MatrixError("Row/column length error in AddScaled");
  57.    while (i--) { *s += *s1 * r; s += d; s1 += d1; }
  58. }
  59.  
  60. void RectMatrixRowCol::Divide(const RectMatrixRowCol& rmrc, real r)
  61. {
  62.    int i = n; real* s = store; int d = spacing;
  63.    real* s1 = rmrc.store; int d1 = rmrc.spacing;
  64.    if (i!=rmrc.n) MatrixError("Row/column length error in Divide");
  65.    while (i--) { *s = *s1 / r; s += d; s1 += d1; }
  66. }
  67.  
  68. void RectMatrixRowCol::Divide(real r)
  69. {
  70.    int i = n; real* s = store; int d = spacing;
  71.    while (i--) { *s /= r; s += d; }
  72. }
  73.  
  74. void RectMatrixRowCol::Negate()
  75. {
  76.    int i = n; real* s = store; int d = spacing;
  77.    while (i--) { *s = - *s; s += d; }
  78. }
  79.  
  80. void RectMatrixRowCol::Zero()
  81. {
  82.    int i = n; real* s = store; int d = spacing;
  83.    while (i--) { *s = 0.0; s += d; }
  84. }
  85.  
  86. void ComplexScale(RectMatrixCol& U, RectMatrixCol& V, real x, real y)
  87. {
  88.    int n = U.n;
  89.    if (n != V.n) MatrixError("Columns different length in ComplexScale");
  90.    real* u = U.store; real* v = V.store; 
  91.    int su = U.spacing; int sv = V.spacing;
  92.    while (n--)
  93.    {
  94.       real z = *u * x - *v * y;  *v =  *u * y + *v * x;  *u = z;
  95.       u += su;  v += sv;
  96.    }
  97. }
  98.  
  99. void Rotate(RectMatrixCol& U, RectMatrixCol& V, real tau, real s)
  100. {
  101.    //  (U, V) = (U, V) * (c, s)  where  tau = s/(1+c), c^2 + s^2 = 1
  102.    int n = U.n;
  103.    if (n != V.n) MatrixError("Columns different length in Rotate");
  104.    real* u = U.store; real* v = V.store; 
  105.    int su = U.spacing; int sv = V.spacing;
  106.    while (n--)
  107.    {
  108.       real zu = *u; real zv = *v;
  109.       *u -= s * (zv + zu * tau); *v += s * (zu - zv * tau);
  110.       u += su;  v += sv;
  111.    }
  112. }
  113.  
  114.  
  115.